/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：DirectiveUtil.java
 * History:
 *         2012-7-5: Initially created, Chrise.
 */
package com.xteam.ecsystem.utils;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import freemarker.template.TemplateBooleanModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateNumberModel;
import freemarker.template.TemplateScalarModel;

/**
 * 标签工具类。
 * @author Chrise
 */
public class DirectiveUtil {
	
	/**
	 * 读取字符串参数。
	 * @author Chrise 2012-7-5
	 * @param params
	 * @param name
	 * @return
	 * @throws TemplateModelException
	 */
	public static String getString(Map<String, TemplateModel> params, String name) throws TemplateModelException {
		TemplateModel param = params.get(name);
		if (param == null) return null;
		
		if (param instanceof TemplateScalarModel) {
			return ((TemplateScalarModel)param).getAsString();
		} else if (param instanceof TemplateNumberModel) {
			return String.valueOf(((TemplateNumberModel)param).getAsNumber());
		} else if (param instanceof TemplateBooleanModel) {
			return String.valueOf(((TemplateBooleanModel)param).getAsBoolean());
		} else {
			throw new TemplateModelException("The \"" + name + "\" parameter must be a string.");
		}
	}
	
	/**
	 * 读取整型参数。
	 * @author Chrise 2012-7-5
	 * @param params
	 * @param name
	 * @return
	 * @throws TemplateModelException
	 */
	public static Integer getInteger(Map<String, TemplateModel> params, String name) throws TemplateModelException {
		TemplateModel param = params.get(name);
		if (param == null) return null;
		
		if (param instanceof TemplateScalarModel) {
			String value = ((TemplateScalarModel)param).getAsString();
			if (StringUtils.isBlank(value)) return null;
			
			try {
				return Integer.parseInt(value);
			} catch (NumberFormatException e) {
				throw new TemplateModelException("The \"" + name + "\" parameter must be a number.");
			}
		} else if (param instanceof TemplateNumberModel) {
			return ((TemplateNumberModel)param).getAsNumber().intValue();
		} else if (param instanceof TemplateBooleanModel) {
			return (((TemplateBooleanModel)param).getAsBoolean()) ? 1 : 0;
		} else {
			throw new TemplateModelException("The \"" + name + "\" parameter must be a number.");
		}
	}
	
	/**
	 * 读取长整型参数。
	 * @author Chrise 2012-7-5
	 * @param params
	 * @param name
	 * @return
	 * @throws TemplateModelException
	 */
	public static Long getLong(Map<String, TemplateModel> params, String name) throws TemplateModelException {
		TemplateModel param = params.get(name);
		if (param == null) return null;
		
		if (param instanceof TemplateScalarModel) {
			String value = ((TemplateScalarModel)param).getAsString();
			if (StringUtils.isBlank(value)) return null;
			
			try {
				return Long.parseLong(value);
			} catch (NumberFormatException e) {
				throw new TemplateModelException("The \"" + name + "\" parameter must be a number.");
			}
		} else if (param instanceof TemplateNumberModel) {
			return ((TemplateNumberModel)param).getAsNumber().longValue();
		} else if (param instanceof TemplateBooleanModel) {
			return (((TemplateBooleanModel)param).getAsBoolean()) ? 1L : 0L;
		} else {
			throw new TemplateModelException("The \"" + name + "\" parameter must be a number.");
		}
	}
	
	/**
	 * 读取布尔参数。
	 * @author Chrise 2012-7-5
	 * @param params
	 * @param name
	 * @return
	 * @throws TemplateModelException
	 */
	public static Boolean getBoolean(Map<String, TemplateModel> params, String name) throws TemplateModelException {
		TemplateModel param = params.get(name);
		if (param == null) return null;
		
		if (param instanceof TemplateScalarModel) {
			String value = ((TemplateScalarModel)param).getAsString();
			return (!StringUtils.isBlank(value) && "1|t|T|y|Y|true|True|TRUE|yes|Yes|YES".indexOf(value) != -1);
		} else if (param instanceof TemplateNumberModel) {
			return !(((TemplateNumberModel)param).getAsNumber().intValue() == 0);
		} else if (param instanceof TemplateBooleanModel) {
			return ((TemplateBooleanModel)param).getAsBoolean();
		} else {
			throw new TemplateModelException("The \"" + name + "\" parameter must be a boolean.");
		}
	}
}
